package com.longagoewk.springboot.service;

import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@Service
public class CommonService {

    //上传图片
    public void UploadPictures(MultipartFile file,String type, String folder, String fileName) throws IOException {
//        String filePath = ResourceUtils.getURL("src").getPath()+"main/resources/image/"+folder+"/";
//        String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"image/"+folder+"/";
        String filePath = ResourceUtils.getURL("File").getPath()+type+"/"+folder+"/";
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(filePath + fileName)));
            bufferedOutputStream.write(bytes);
            bufferedOutputStream.close();
        }
    }

}
